#include "usershow.h"
#include "ui_usershow.h"

userShow::userShow(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::userShow),m_defaultImg(":/bq/000.png")//设置无视频源默认显示图片
{
    ui->setupUi(this);
    m_lastTime = QTime::currentTime();//初始化最后时间为创造时间
    //usershow的定时器m_timer发送一个超时信号，这个usershow就要去检测接收界面是否超时
    //超时设置为默认画面
    //This signal is emitted when the timer times out.
    //Note: This is a private signal. It can be used in signal connections,
    //but cannot be emitted by the user.
    //当计时器超时时发出此信号。
    //注意：这是私人信号。它可以在信号连接中使用，但不能由用户发射。
    connect(&m_timer,SIGNAL(timeout()),
            this,SLOT(slot_checkTimer()));
    m_timer.start();
}

userShow::~userShow()
{
    delete ui;
}

//设置这个usershow的个人信息，id和name
void userShow::slot_setInfo(int id, QString name)
{
    m_id = id;
    m_userName = name;

    //标题显示用户名称
    QString text = QString("用户名:%1").arg(m_userName);
    ui->lb_name->setText(text);
}

//重写重绘事件，绘图图片源为m_img
void userShow::paintEvent(QPaintEvent *event)
{
    //画黑色背景
    QPainter painer(this);
    painer.setBrush(Qt::black);//设置黑色画刷
    painer.drawRect(0,0,this->width(),this->height());//设置矩形面积

    if(m_img.size().height() <= 0)
    {
        return;
    }

    //画视频帧
    //等比例缩放
    m_img = m_img.scaled(this->width() , this->height() - ui->lb_name->height() , Qt::KeepAspectRatio);
    QPixmap pix = QPixmap::fromImage(m_img);
    int x = this->width() - pix.width();
    x /= 2;
    int y = this->height() - pix.height() - ui->lb_name->height();
    y /= 2;
    y += ui->lb_name->height();

    //Draws the rectangular portion source of the given pixmap
    //into the given target in the paint device.
    //将给定像素映射的矩形部分源绘制到绘制设备中的给定目标中。
    painer.drawPixmap(QPoint(x,y) , pix);

    //Ends painting. Any resources used while painting are released.
    //You don't normally need to call this since it is called by the destructor.
    //Returns true if the painter is no longer active; otherwise returns false
    //结束绘制。绘制时使用的任何资源都将被释放。
    //你通常不需要调用这个，因为它是由析构函数调用的。
    //如果绘制器不再处于活动状态，则返回真；否则返回假
    painer.end();
}

//将这个userShow的图片m_img设置为img
void userShow::slot_setImage(QImage &img)
{
    m_img = img;
    //每次获得画面，更新上次时间
    m_lastTime = QTime::currentTime();
    this->update();//触发重绘
}

//点击了右侧的usershow窗口，就发送一个信号，使得roomdlg的主窗口为这个usershow窗口
void userShow::mousePressEvent(QMouseEvent* event)
{
    event->accept();
    Q_EMIT SIG_itemClicked(m_id,m_userName);
}

//定时检测 接收界面是否超时
void userShow::slot_checkTimer()
{
    if( m_lastTime.secsTo(QTime::currentTime()) >5)
    {
        //设为默认画面
        slot_setImage(m_defaultImg);
    }
}
